ExoLab     OpenEJB     OpenJMS     OpenORB     Castor     Tyrex     
 

Main
  Home
  About
  Features
  Download
  API
  Schema
  Castor Forums
  Mailing Lists
  CVS / Bugzilla
  Support
  CastorWiki

XML
  Using XML
  Source Generator
  Schema Support
  XML Mapping
  XML FAQ

JDO
  Using JDO
  JDO Config
  Types
  JDO Mapping
  JDO FAQ
  Other Features

Advanced JDO
  OQL
  Trans. & Locks
  Design
  KeyGen
  Long Trans.
  Nested Attrs.
  Pooling Examples
  LOBs

More
  Presentations
  The Examples
  3rd Party Tools
  JDO Tests
  XML Tests
  Configuration
  Tips & Tricks
  Full JavaDoc
  CastorWiki
  
  

About
  License
  Contributors
  Status, Todo
  Changelog
  Library
  Contact
  Project Name

  



The Source Code Generator

Reference: The Source Builder API

For a more detailed version of the Source Generator documentation, please download the Source Generator User Document(PDF).

Introduction
Source Generator Options
    Command Line Options
        Collection Types
    Advanced Options
        Bound Properties
        Class Creation/Mapping
        The 'element' method
        The 'type' method
        Setting a super class
        Mapping XML namespaces to Java packages
        Generate equals() method
        Maps java primitive types to wrapper object
Binding File
    <binding> element
    <include> element
    <package> element
    <namingXML> element
    <componentBinding> element
    <java-class> element
    <interface> element
    <member> element
    todo
XML Schema

Introduction

Castor's Source Code Generator creates a set of Java classes which represent an object model for an XML Schema (W3C XML Schema, 20010502 Recommendation)1, as well as the necessary Class Descriptors used by the marshalling framework to obtain information about the generated classes. Currently the generated source files will need to be compiled. We will be adding an Ant taskdef eventually to handle this automatically.

Example:

java org.exolab.castor.builder.SourceGenerator -i foo-schema.xsd -package com.xyz

This will generate a set of source files from the the XML Schema, foo-schema.xsd, and place them in the com/xyz/* package.

To compile the generated classes simply run javac, or your favorite compiler:

javac com/xyz/*.java

Created class will have marshal and unmarshal methods which are used to go back and forth between XML and an Object instance.

Source Generator Options

The source code generator has a number of different options which may be set. Some of these are done using the command line, and others are done using a properties file located at

"org/exolab/castor/builder/castorbuilder.properties"
.

Command Line Options

OptionArgsDescriptionOptional?
ifilename The input XML Schema file Required
packagepackage-name The package for the generated source Optional
destpath The destination in which to put the generated source Optional
line-separatorunix | mac | win Sets the line separator style for the desired platform. This is useful if you are generating source on one platform, but will be compiling/modifying on another platform. Optional
typestype-factory Sets which type factory to use. This is useful if you you want JDK 1.2 collections instead of JDK 1.1 or if you want to pass in your own FieldInfoFactory. (see Collection types) Optional
h
Shows the help/usage screen Optional
f
Forces the source generator to supress all non-fatal errors, such as overwriting of pre-existing files. Optional
nodesc
Do not generate the class descriptors Optional
nomarshall
Do not generate the marshalling framework methods (marshall, unmarshall, validate) Optional
testable
Generate the specific methods used by the Castor Marshalling Framework Optional
sax1
Generate marshalling methods that are using SAX1 framework (default is false). Optional
binding-fileThe binding file name. Allows the setting of a Binding File to allow a fine-grained control the classes generated Optional
generateImportedSchemas
Generates automatically sources for imported XML Schemas (default is false). Optional

Collection Types

The source code generator has the ability to use the following types of collections when generating source code:

-Java 1.1 (default): java.util.Vector
-Java 1.2: use the option -types j2, collection type is java.util.Collection
-ODMG 3.0: use the option -types odmg, collection tyoe is odmg.DArray
You can also write your own FieldInfoFactory to handle specific collection types. All you have to do is to pass in the fully qualified name of that FieldInfoFactory: -types com.personal.MyCoolFactory

Advanced Options

Bound Properties

Since version: 0.8.9

Bound properties are "properties" of a class, which when updated the class will send out a java.beans.PropertyChangeEvent to all registered java.beans.PropertyChangeListeners.

To enable bound properties uncomment the appropriate line in the "org/exolab/castor/builder/castorbuilder.properties" file:

    # To enable bound properties uncomment the following line. Please
    # note that currently *all* fields will be treated as bound properties
    # when enabled. This will change in the future when we introduce
    # fine grained control over each class and it's properties.
    #
    #org.exolab.castor.builder.boundproperties=true
               

When enabled, all properties will be treated as bound properties. For each class that is generated a setPropertyChangeListener method is created as follows:

    
    /**
     * Registers a PropertyChangeListener with this class.
     * @param pcl The PropertyChangeListener to register.
    **/
    
    public void addPropertyChangeListener(java.beans.PropertyChangeListener pcl)
    {
        propertyChangeListeners.addElement(pcl);
    } //-- void addPropertyChangeListener(java.beans.PropertyChangeListener)
                 
Whenever a property of the class is changed, a PropertyChangeEvent will be sent to all registered listeners. The property name, the old value, and the new value will be set in the PropertyChangeEvent.

Note: To prevent unnecessary overhead, if the property is a collection, the old value will be null.

Class Creation/Mapping

Since version: 0.8.9

The source generator can treat the XML Schema structures such as complexType and element in two main ways. The first, and currently default method is called the "element" method. The other is called the "type" method.

The 'element' method

The "element" method creates classes for all elements whose type is a complexType. Abstract classes are created for all top-level complexTypes. Any elements whose type is a top-level type will have a new class create that extends the abstract class which was generated for that top-level complexType.

Classes are not created for elements whose type is a simpleType.

The 'type' method

The "type" method creates classes for all top-level complexTypes, or elements that contain an "anonymous" (in-lined) complexType.

Classes will not be generated for elements whose type is a top-level type.

More information on this is forth coming.

To change the "method" of class creation simple edit the castorbuilder.properties file:

    # Java class mapping of <xsd:element>'s and <xsd:complexType>'s
    #
    #org.exolab.castor.builder.javaclassmapping=element
             

Setting a super class

Since version: 0.8.10

The source generator enables the user to set a super class to all the generated classes (of course class descriptors are not concerned by this option)

To set up the super class, edit the castorbuilder.properties file :

    # This property allows one to specify the super class of *all*
    # generated classes
    #
    #org.exolab.castor.builder.superclass=com.xyz.BaseObject
            

Mapping XML namespaces to Java packages

Since version: 0.9.0

An XML Schema instance is identified by a namespace. For data-binding purposes, especially code generation it may be necessary to map namespaces to Java packages.

This is needed for imported schema in order for Castor to generate the correct imports during code generation for the primary schema.

To allow the mapping between namespaces and Java packages , edit the castorbuilder.properties file :

	 # XML namespace mapping to Java packages
         #
         #org.exolab.castor.builder.nspackages=\
               http://www.xyz.com/schemas/project=com.xyz.schemas.project,\
               http://www.xyz.com/schemas/person=com.xyz.schemas.person
            

Generate equals() method

Since version: 0.9.1

The Source Generator can override the 'equals()' method for the generated objects.

Note: hashcode() is not currently overriden.

To generate the equals() method , edit the castorbuilder.properties file :

	# Set to true if you want to generate the equals method
	# for each generated class.
	# false by default
	#org.exolab.castor.builder.equalsmethod=true

            

Maps java primitive types to wrapper object

Since version 0.9.4

It may be convenient to use java objects instead of primitives, the Source Generator provides a way to do it. Thus the following mapping can be used:

-boolean to java.lang.Boolean
-byte to java.lang.Byte
-double to java.lang.Double
-float to java.lang.Float
-int and integer to java.lang.Integer
-long to java.lang.Long
-short to java.lang.Short

To enable this property, edit the castor builder.properties file:

	 # Set to true if you want to use Object Wrappers instead
	 # of primitives (e.g Float instead of float).
	 # false by default.
	 #org.exolab.castor.builder.primitivetowrapper=false
	     

Binding File

It may appear that the default binding used to generate the Java Object Model from an XML schema doesn't meet one's expectation. For instance, the default binding won't deal with naming collision problems that can appear since XML Schema allows an element declaration and a complexType definition to both share the same name. From a Java standpoint, it will result in the creation of two classes with the same qualified name: the second class generated will simply overwrite the first one. Another example is when one user wants to change the default datatype binding provided by Castor or to add validation rules by implementing his own validator and passing it to the SourceGenerator.

The Binding declaration is an XML based language that allows the user to define such control on the generated classes. The aim of this section is to provide an overview of the binding file as well as a definition of the several XML components used to define this binding file. A more in-depth presentation will be available soon in the Source Generator User Document(PDF).

<binding> element

<binding
    defaultBindingType = (element|type)>
    (include*,package*,namingXML?,elementBinding*, attributeBinding, complexTypeBinding, groupBinding)
</binding>
               

The binding element is the root element and contains the binding information. The attribute

defaultBindingType
controls the Class creation type

<include> element

<include
    URI = xsd:anyURI/>
               
-URI:The URI of the binding file to include.

This element allows one to include the binding declaration defined in another file. This allows re-usability of Binding files defined for various XML Schemas.

<package> element

<package>
    name = xsd:string
    (namespace|schemaLocation) = xsd:string>
</package>

               
-name:A fully qualified java package name.
-namespace:An XML namespace that will be mapped to the package name defined by the name element.
-schemaLocation:A URL that locates the schema to be mapped to the package name defined by the name element.

The targetNamespace attribute of an XML Schema identifies the namespace in which the XML language is defined. Such language namespace is defined in the java language as package declaration. The <package/> element allows you to define the mapping between an XML namespace and a Java package. Moreover XML Schema allows you to factorize the definition of an XML Schema identified by a unique namespace by including several XML Schemas to build one XML Schema using the <xsd:include/> element (Please make sure you understand the difference between <xsd:include/> and <xsd:import/>). <xsd:include/> relies on the URI of the included XML schema and it can be needed to keep the structure hierarchy defined in XML Schema in the Java package generated. Thus the binding file allows to define the mapping between a schemaLocation attribute and a Java package.

<namingXML> element

<namingXML>
   (elementName,complexTypeName,modelGroupName)
</namingXML>

<elementName|complexTypeName|modelGroupName>
    (prefix?, suffix?) = xsd:string
</elementName|complexTypeName|modelGroupName>

               
-prefix:The prefix to add to the names of the generated classes.
-suffix:The suffix to append to the the names of the generated classes

One of the aim of the binding file is to avoid naming collisions. Indeed XML Schema allows elements and complexTypes to share the same name which results in name collisions when generated the sources. Defining a binding for every element and complexType that share the same name is sometimes not a convenient solution (for instance the BPML XML Schema or the UDDI v2.0 XML Schema use the same names for top-level complexTypes and top-level elements). The aim of the <naming/> XML element is to define a prefix and a suffix for the names of the classes generated for an element, a complexType or a model group definition.
Note:It is not possible to control the names of the classes generated to represent nested model groups (all, choice, sequence).

<componentBinding> element

<elementBinding|attributeBinding|complexTypeBinding|groupBinding
    name = xsd:string>
   ((java-class|interface|member), elementBinding*, attributeBinding*, complexTypeBinding*, groupBinding*)
</elementBinding|attributeBinding|complexTypeBinding|groupBinding>

               
-name:The name of the XML schema component for which we are defining a binding.

These elements are the tenets of the binding file since they contain the binding definition for an XML Schema element, attribute, complexType and modelGroup definition. The first child element (<java-class/>, <interface> or <member>) will determine the type of binding one is defining. Please note that defining a <java-class> binding on an XML Schema attribute will have absolutely no effect.

The binding file being written from an XML Schema point of view; there are two distinct ways to define the XML Schema component for which we are defining a binding. First we can define it throught the name attribute.

The value of the name attribute uniquely identifies the XML Schema Component. It can refer to the top-level component using the NCName of that component or it can use a location language based on XPath3. The grammar of that language can be defined by the following BNF:

              [1]Path         ::= LocationPath('/'LocationPath)*
              [2]LocationPath ::= (Complex|ModelGroup|Attribute|Element)
              [3]Complex      ::= 'complexType:'NCName
              [4]ModelGroup   ::= 'group:'NCName
              [5]Attribute    ::= '@'NCName
              [6]Element      ::= NCName
          

The second option to identify a XML Schema Component is to embed its binding definition inside its parent binding definition.

For instance, the following binding definition will be equivalent and will identify the element 'foo' defined in the top-level complexType 'fooType'.

             <elementBinding name="complexType:fooType/foo>
                 <member name="MyFoo" handler="mypackage.myHandler"/>
             </elementBinding>
          
             <complexTypeBinding name="fooType">
                <elementBinding name="foo>
                    <member name="MyFoo" handler="mypackage.myHandler"/>
               </elementBinding>
             <complexTypeBinding>
          

<java-class> element

<java-class
    name? = xsd:string
    package? = xsd:string
    final? = xsd:boolean
    abstract? = xsd:boolean
    equals? = xsd:boolean
    bound? = xsd:boolean >
    (implements*,extends?)
</java-class>

               
-name:The name of the class that will be generated.
-package:The package of the class to be generated. if set, this option overrides the mapping defined in the <package/> element.
-final:If true, the generated class will be final.
-abstract:If true, the generated class will be abstract.
-equals:If true, the generated class will implement the equals method.
-bound:If true, the generated class will implement the bound properties.

This element defines all the options for the class to be generated.

<interface> element

<interface>
    name = xsd:string
</interface>

               
-name:The name of the interface that will be generated.

This element specifies the name of the interface to be generated for an XML schema component.

NOTE: this element is not yet implemented.

<member> element

 <member
     name? = xsd:string
     java-type? = xsd:string
     wrapper? = xsd:boolean
     handler? = xsd:string
     collection? = (array|vector|arraylist|hashtable|collection|odmg|set|map)
     validator? = xsd:string/>


               
-name:The name of the class member that will be generated.
-java-type:the fully qualified name of the java type.
-wrapper:If true, a wrapper object will be generated in case the java type is a java primitive.
-handler:The fully qualified name of the FieldHandler to use.
-collection:If the schema component can occur more than once then this attribute allows to specify the collection to use to represent the component in Java.
-validator:The fully qualified name of the FieldValidator to use.

This element represents the binding for class member. It allows the definition of its name and java type as well as an implementation of FieldHandler to help the Marshalling framework in handling that member. Defining a validator is also possible. The names given for the validator and the fieldHandler must be fully qualified.

todo

-
 <enumBinding>
    (enumClassName|enumMemberName)
 </enumBinding>

                  

This element will allow more control on the type safe enumerations generated to represent an XML Schema simpleType enumeration

-
<javadoc/>

the javadoc element will allow one to enter the necessary javadoc that represents the generated classes or members.

XML Schema

The input file to the source code generator is an XML Schema2. The current supported version is the W3C XML Schema Recommendation1. For more information about XML Schema Support, check the XML Schema page


1Castor 0.9.4.1 uses XML Schema Recommendation 20010502

2XML Schema is a W3C Recommendation

3XPath is a W3C Recommendation
 
   
  
   
 


Copyright © 1999-2004 ExoLab Group. All rights reserved.
 
Java, EJB, JDBC, JNDI, JTA, Sun, Sun Microsystems are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and in other countries. XML, XML Schema, XSLT and related standards are trademarks or registered trademarks of MIT, INRIA, Keio or others, and a product of the World Wide Web Consortium. All other product names mentioned herein are trademarks of their respective owners.